<?php
	ini_set('display_errors',1);
	header('Content-Type: text/html; charset=utf-8');

	var_dump(
		array(
			'loc'=>array( '$near' => array(31,1221) )
		)
	);
	

	require_once 'time_diff.php';
	
	echo "geo tutorial";
	//上海市 嘉定区 曹安公路 2089弄 6号 
	//http://www.mongodb.org/display/DOCS/Geospatial+Indexing
	//http://www.i-marketing.asia/resources_latitude-longitude-generator.php
	/*
	1	中国上海市嘉定区嘉怡路2号	31.259109654357527	121.34710550308228
	2	中国上海市嘉定区华江公路3号	31.25877948610676	121.34676218032837
	3	中国上海市嘉定区曹安公路2114号-2144号	31.258724457952656	121.34749174118042
	4	中国上海市嘉定区曹安公路2079号	31.25846765947609	121.34721279144287
	5	中国上海市嘉定区嘉怡路2号-100号	31.260191864415813	121.34633302688599
	6	中国上海市嘉定区曹安公路2020号	31.25898125573057	121.34940147399902
	7	中国上海市嘉定区建华路88号	31.256688393698674	121.34839296340942
	8	中国上海市嘉定区华江公路30号	31.258100803297616	121.34453058242798
	9		中国上海市嘉定区曹安公路2146号-2258号	31.26046700058782	121.34476661682129
	10	中国上海市嘉定区华江公路25号	31.257385429648544	121.34629011154
	
	*/
	$before = time();
	for ($i=0; $i<1000; $i++) {
	  $m = new Mongo("localhost:27017", array("persist" => "x"));
	}
	$after = time();
	echo time2Units($diff)."<br/>";
	
	// connect				
	$m = new Mongo("localhost:27017", array("persist" => "x"));

	// select a database
	$db = $m->gshow;

	// select a collection (analogous to a relational database's table)
	$collection = $db->geos;

	// no1 user
	$obj = array( 
		/*
		"loc" => array(
			"lat" => 31.259109654357527, 
			"long" => 121.34710550308228
		),*/
		
		"loc" => array(31.259109654357527,121.34710550308228),
		"uid"  => 1, 
		"uuid" => uniqid(), 
		"time" => time(), 
	
		
		"address" => "中国上海市嘉定区嘉怡路2号	31", 

		"description" => "no description",

	);
	$collection->insert($obj);
	
	
	// no2 user
	$obj = array( 
		"loc" => array(31.25877948610676,121.34676218032837),

		"uid"  => 2, 
		"uuid" => uniqid(), 
		"time" => time(), 

		"address" => "中国上海市嘉定区华江公路3号", 

		"description" => "no description",

	);
	$collection->insert($obj);
	
	
	// no3 user
	$obj = array( 
		"loc" => array(31.258724457952656,121.34749174118042),

		"uid"  => 3, 
		"uuid" => uniqid(), 
		"time" => time(), 


		"address" => "中国上海市嘉定区曹安公路2114号-2144号", 

		"description" => "no description",

	);
	$collection->insert($obj);
	
	
	
	// no4 user
	$obj = array( 
		"loc" => array(31.258632744291244,121.3473629951477),
	
		"uid"  => 4, 
		"uuid" => uniqid(), 
		"time" => time(), 

 
		"address" => "中国上海市嘉定区曹安公路2079号", 

		"description" => "no description",

	);
	$collection->insert($obj);
	
	
	
	// no5 user
	$obj = array( 
		"loc" => array(31.25898125573057,121.34940147399902),
		
		"uid"  => 5, 
		"uuid" => uniqid(), 
		"time" => time(), 


		"address" => "中国上海市嘉定区曹安公路2020号", 

		"description" => "no description",

	);
	$collection->insert($obj);
	
	
	
	// no6 user
	$obj = array( 
		"loc" => array(31.256688393698674,121.34839296340942),
		
		"uid"  => 6, 
		"uuid" => uniqid(), 
		"time" => time(), 


		"address" => "中国上海市嘉定区建华路88号", 

		"description" => "no description",

	);
	$collection->insert($obj);
	
	// no7 user
	$obj = array( 
			"loc" => array(31.258100803297616,121.34453058242798),
			

			"uid"  => 7, 
			"uuid" => uniqid(), 
			"time" => time(), 
	

			"address" => "中国上海市嘉定区华江公路30号", 
	
			"description" => "no description",
	
	);
	$collection->insert($obj);
		
		
	// no8 user
	$obj = array( 
			"loc" => array(31.257385429648544,121.34629011154),

			"uid"  => 8, 
			"uuid" => uniqid(), 
			"time" => time(), 
	

			"address" => "中国上海市嘉定区华江公路25号", 
	
			"description" => "no description",
	
	);
	$collection->insert($obj);
	
	// find everything in the collection
	$cursor = $collection->find(
		 array(
			"loc"=>array( '$near' => array(31,121) )
		 )
	)->limit(50);
	
	//$cursor = $db->command(array('geoNear'=>'geos','near'=>array(31,121),'num'=>9));
	
	//$cursor = $collection->find(Array('loc' => Array('$near' => array(31,121) )));	
	//var_dump($cursor). "\n";
	// iterate through the results
	foreach ($cursor as $obj) {
	    var_dump($obj). "\n";
	}
		
	//db.geos.find({loc : {$near : [31,121]}})
	/*
	
	db.geos.ensureIndex({"loc" : "2d"},{"background" : true})
	db.geos.getIndexes()  
	
	
	注意了 drop之后 会报没索引 其实是没数据 注意
	
	
	查询测试,返回结果按照从最近到最远的顺序排序输出.
	
	*/
	
	//db.geos.find({loc : {$near : [31,121]},"uid" : 1}).explain()